//
//  LogManager.h
//  TRKUTIL
//
//  Created by 李通洲 on 15/1/24.
//  Copyright (c) 2015年 Wicresoft. All rights reserved.
//

#pragma once

#ifndef _CRT_SECURE_NO_WARNINGS
#   define _CRT_SECURE_NO_WARNINGS 1
#endif

#include <string>
#include <ctime>
#include "posix.h"

enum LogLevel {
    Unknown,
    Info,
    Warning,
    Error,
    None,
};

class LogManager {
    static LogManager *instance;
    
    fmt::BufferedFile __flog;  // Use getLogFile() instead
    std::time_t _t;
    std::string _fileName;
    
public:
    LogLevel GlobalLogLevel;
    
private:
    LogManager(fmt::StringRef fileName, std::time_t t);
    LogManager(const LogManager&);  // = delete
    
    fmt::BufferedFile& getLogFile();
    void printTime();
    
    ~LogManager();
    
    static void Destruct() {
        delete instance;
        instance = NULL;
    }
    
public:
    static LogManager& getInstance();
    
public:
    void WriteLog(fmt::StringRef msg, LogLevel level);
    
    void LogInfo(fmt::StringRef msg) {
        WriteLog(msg, Info);
    }
    
    void LogWarning(fmt::StringRef msg) {
        WriteLog(msg, Warning);
    }
    
    void LogError(fmt::StringRef msg) {
        WriteLog(msg, Error);
    }
};

#ifdef _MSC_VER
_declspec(noreturn)
#else
__attribute__((__noreturn__))
#endif
void panic(fmt::StringRef msg, int errorCode = 1);

